package br.com.krauchenco.resourcemanager;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.logging.Logger;

public class PropertiesWrapper extends Properties {
	/** Logger para mensagens */
	static final Logger logger = Logger.getLogger(PropertiesWrapper.class
			.getName());
	private static final long serialVersionUID = 1L;
	private static final String COMMENT = null;
	private OutputStream out;

	public PropertiesWrapper(File file) throws FileNotFoundException, IOException {
		super();
		if (!file.exists()) file.createNewFile();
		load(new FileInputStream(file));
		out = new FileOutputStream(file);
	}

	public void setProperty(String key, int value) throws IOException {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = String.valueOf(value);
		setProperty(key, valueStr);
		if (null != out) {
			store(out, COMMENT);
		}
	}
	
	public void setProperty(String key, long value) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = String.valueOf(value);
		setProperty(key, valueStr);
	}
	
	public void setProperty(String key, boolean value) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = String.valueOf(value);
		setProperty(key, valueStr);
	}
	
	public void setProperty(String key, float value) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = String.valueOf(value);
		setProperty(key, valueStr);
	}
	
	public int getProperty(String key, int valueDefault) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = getProperty(key);
		try {
			return Integer.parseInt(valueStr);
			
		} catch (NumberFormatException nfEx) {
			logger.severe("Erro ao executar o parse de \"" + valueStr  //$NON-NLS-1$
					+ "\" para int, da propriedade \"" + key  //$NON-NLS-1$
					+ "\", por isso o valor default \"" + valueDefault  //$NON-NLS-1$
					+ "\" sera utilizado!"); //$NON-NLS-1$
			setProperty(key, valueDefault);
			return valueDefault;
		}
	}
	
	public long getProperty(String key, long valueDefault) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = getProperty(key);
		try {
			return Long.parseLong(valueStr);
			
		} catch (NumberFormatException nfEx) {
			logger.severe("Erro ao executar o parse de \"" + valueStr  //$NON-NLS-1$
					+ "\" para long, da propriedade \"" + key  //$NON-NLS-1$
					+ "\", por isso o valor default \"" + valueDefault  //$NON-NLS-1$
					+ "\" sera utilizado!"); //$NON-NLS-1$
			setProperty(key, valueDefault);
			return valueDefault;
		}
	}
	
	public boolean getProperty(String key, boolean valueDefault) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = getProperty(key);
		try {
			return Boolean.parseBoolean(valueStr);
			
		} catch (NumberFormatException nfEx) {
			logger.severe("Erro ao executar o parse de \"" + valueStr  //$NON-NLS-1$
					+ "\" para boolean, da propriedade \"" + key  //$NON-NLS-1$
					+ "\", por isso o valor default \"" + valueDefault  //$NON-NLS-1$
					+ "\" sera utilizado!"); //$NON-NLS-1$
			setProperty(key, valueDefault);
			return valueDefault;
		}
	}
	
	public float getProperty(String key, float valueDefault) {
		if (null == key || key.trim().length() == 0) {
			throw new IllegalArgumentException("key esta vazio"); //$NON-NLS-1$
		}
		String valueStr = getProperty(key);
		try {
			return Float.parseFloat(valueStr);
			
		} catch (NumberFormatException nfEx) {
			logger.severe("Erro ao executar o parse de \"" + valueStr  //$NON-NLS-1$
					+ "\" para float, da propriedade \"" + key  //$NON-NLS-1$
					+ "\", por isso o valor default \"" + valueDefault  //$NON-NLS-1$
					+ "\" sera utilizado!"); //$NON-NLS-1$
			setProperty(key, valueDefault);
			return valueDefault;
		}
	}
	
	public static void main(String[] args) throws IOException {
		PropertiesWrapper pw = new PropertiesWrapper(new File("teste")); //$NON-NLS-1$
		pw.setProperty("key", "value");  //$NON-NLS-1$//$NON-NLS-2$
		pw.setProperty("int", 5); //$NON-NLS-1$
		pw.setProperty("long", 5l); //$NON-NLS-1$
		pw.setProperty("boolean", false); //$NON-NLS-1$
		pw.setProperty("float", 1f); //$NON-NLS-1$
		
		pw.getProperty("key", "value");  //$NON-NLS-1$//$NON-NLS-2$
		pw.getProperty("int", 5); //$NON-NLS-1$
		pw.getProperty("long", 5l); //$NON-NLS-1$
		pw.getProperty("boolean", false); //$NON-NLS-1$
		pw.getProperty("float", 1f); //$NON-NLS-1$
	}
}
